package com.frx.datastruct.chapterfive;

/**
 * @author ：frx
 * @date ：Created in 2019/2/19 22:16
 * @description： 使用虚拟头节点解决问题
 * @modified By：
 * @version: ToDO
 */
public class Solution2 {

    public ListNode removeElements(ListNode head,int val){

        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;

        ListNode prev = dummyHead;
        while(prev.next != null){
            if (prev.next.val == val){
                ListNode delNode = prev.next;
                prev.next = delNode.next;
                delNode.next = null;
            } else {
                prev = prev.next;
            }
        }
        return dummyHead.next;
    }
    public static void main(String[] args){
        int[] nums = {1,2,6,3,4,5,6};

        ListNode head = new ListNode(nums);
        System.out.println(head);
        ListNode node = new Solution2().removeElements(head,6);
        System.out.println(node);
    }
}
